package cn.ml.io.liu;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamTest {
    public static void main(String[] args){
        FileOutputStream fos=null;
        try {
            //1、向文件中覆盖(相当于先删除再写入)
            fos=new FileOutputStream("D:\\1_JAVA\\java_long\\basis\\src\\cn\\ml\\chuji\\io\\liu\\hello.txt");
            //2、向文件中追加(在源文件末尾写入)
            fos=new FileOutputStream("D:\\1_JAVA\\java_long\\basis\\src\\cn\\ml\\chuji\\io\\liu\\hello.txt",true);
            String str="好好学习Java";
            byte[] words=str.getBytes();//将字符串打散为字节数组
            fos.write(words,0,words.length);
            System.out.println("hello文件已更新");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("文件更新时出错！");
            e.printStackTrace();
        }finally{
            try {
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
